package com.wan.adapter

import android.content.Context
import android.support.v4.util.SparseArrayCompat
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.wan.base.BaseBean
import com.wan.uitls.ItemClickListener


/**
 * 通用Holder
 */
class BaseViewHolder(var context: Context, itemView: View) : RecyclerView.ViewHolder(itemView) {

    companion object {
        fun getBaseViewHolder(context: Context, view: View): BaseViewHolder {
            return BaseViewHolder(context, view)
        }
    }

    private var views = SparseArrayCompat<View>(10)

    @Suppress("UNCHECKED_CAST")
    fun <T : View> getView(itemId: Int): T {
        var targetView = views.get(itemId)
        if (targetView == null) {
            targetView = itemView.findViewById(itemId)
            views.put(itemId, targetView)
        }
        return targetView as T
    }

    fun setText(itemId: Int, text: String) {
        var textView = getView<TextView>(itemId)
        textView.text = text
    }

    fun setImageResource(itemId: Int, imgId: Int) {
        var imageView = getView<ImageView>(itemId)
        imageView.setImageResource(imgId)
    }

}

/**
 *通用适配器
 */

abstract class BaseRecyclerAdapter<T>(var context: Context, var data: ArrayList<T>, var itemViewId: Int) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var recyclerView: RecyclerView? = null


    private var itemClickListener: ItemClickListener? = null
    fun setItemClickListener(itemClickListener: ItemClickListener) {
        this.itemClickListener = itemClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        var view = LayoutInflater.from(context).inflate(itemViewId, parent, false)
        return BaseViewHolder.getBaseViewHolder(context, view)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is BaseViewHolder) {
            convert(holder, data[position], position)
            if (itemClickListener != null) {
                holder.itemView.setOnClickListener {
                    itemClickListener!!.onItemClickListener(position)
                }
            }
        }
    }

    abstract fun convert(holder: BaseViewHolder, itemEntity: T, position: Int)

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        this.recyclerView = recyclerView
    }

    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        super.onDetachedFromRecyclerView(recyclerView)
        this.recyclerView = null
    }

    fun addListData(newData: ArrayList<T>) {
        this.data.addAll(newData)
        notifyDataSetChanged()
    }

    fun addOneData(newData: T) {
        this.addOneData(data.size, newData)
    }

    fun addOneData(position: Int, newData: T) {
        this.data.add(position, newData)
        notifyItemInserted(position)
    }

    fun remove(position: Int) {
        this.data.removeAt(position)
        notifyItemRemoved(position)
    }

    fun update(position: Int, entity: T) {
        this.data.removeAt(position)
        this.data.add(position, entity)
        notifyItemChanged(position, entity)
    }

    fun getEntity(position: Int): T {
        return data[position]
    }

    fun getTotalData(): ArrayList<T> {
        return this.data
    }
}